package ua.nure.nguyen.blinov.chapter4.a;

import java.util.ArrayList;
import java.util.List;

public class Text {
	private List<Sentence> text;
	
	private Text(List<Sentence> text) {
		this.text = text;
	}
	
	public static Text parseText(String text) {
		if (text == null || text.isEmpty()) {
			return new Text(new ArrayList<Sentence>());
		} else {
			List<Sentence> result = new ArrayList<Sentence>();
			String[] sentences = text.split(".");
			for (String sentence : sentences) {
				result.add(Sentence.parseSentence(sentence));
			}
			return new Text(result);
		}
	}
	
	public Text appends(String sentence) {
		text.add(Sentence.parseSentence(sentence));
		return this;
	}
	
	public Text appends(Sentence sentence) {
		this.text.add(sentence);
		return this;
	}
	
	public Text appends(Text text) {
		for (int i = 0; i < text.text.size(); i++) {
			this.text.add(text.text.get(i));
		}
		return this;
	}
	
	public Sentence getHead() {
		if (text.size() == 0) {
			return null;
		}
		return text.get(0);
	}
	
	@Override
	public boolean equals(Object other) {
		if (other instanceof Text == false) {
			return false;
		}
		List<Sentence> otherText = ((Text)other).text;
		if (text.size() != otherText.size()) {
			return false;
		}
		for (int i = 0; i < text.size(); i++) {
			if (!text.get(i).equals(otherText.get(i))) {
				return false;
			}
		}
		return true;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for (Sentence sentence : text) {
			sb.append(sentence.toString());
		}
		return sb.toString();
	}
}
